package cn.xm.org.service.impl;


import cn.xm.basic.constants.Constant;
import cn.xm.basic.exception.BusinessException;
import cn.xm.basic.result.AjaxResult;
import cn.xm.basic.utils.BaiduAuditUtils;
import cn.xm.basic.vo.ShopStateVo;
import cn.xm.org.domain.Emp;
import cn.xm.org.domain.Shop;
import cn.xm.org.domain.ShopAuditLog;
import cn.xm.org.mapper.EmpMapper;
import cn.xm.org.mapper.ShopAuditLogMapper;
import cn.xm.org.mapper.ShopMapper;
import cn.xm.org.service.ShopService;
import cn.xm.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xm
 * @since 2022-12-17
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender mailSender;

    @Override
    @Transactional
    public void settle(Shop shop) {
        // 参数校验
        // 判断两次密码是否相同
        if (!shop.getAdmin().getConfirmPassword().equals(shop.getAdmin().getConfirmPassword())) {
            throw new BusinessException("两次密码不一致");
        }
        // 判断店铺logo是否合规
        Boolean nameCensor = BaiduAuditUtils.TextCensor(shop.getName());
        if (!nameCensor) {
            throw new BusinessException("店铺名存在违规内容");
        }
        // shop.getlogo = /group/...
        Boolean imgCensor = BaiduAuditUtils.ImgCensor(Constant.IMAGE_BASE_URL + shop.getLogo());
        if (!imgCensor) {
            throw new BusinessException("店铺logo存在违规内容");
        }
        // 判断店铺是够已经存在
        Shop byName = shopMapper.findByName(shop.getName());
        if (byName != null) {
            // 已存在抛异常
            throw new BusinessException("该店铺已经存在");
        }
        shopMapper.add(shop);
        Emp emp = empMapper.findByPhone(shop.getAdmin().getPhone());
        // 判断店铺管理员是否已存在
        if (emp == null) {
            emp = shop.getAdmin();
            emp.setShop(new Shop());
            emp.getShop().setId(shop.getId());
            // 添加该员工
            empMapper.add(emp);
            shop.setAdminId(emp.getId());
            shopMapper.update(shop);
        } else if (emp != null && emp.getShopId() == null) {
            emp.setShop(new Shop());
            emp.getShop().setId(shop.getId());
            empMapper.update(emp);
            shop.setAdminId(emp.getId());
            shopMapper.update(shop);
        } else {
            throw new BusinessException("此人员是其他店铺的员工");
        }
    }

    @Override
    @Transactional
    public void auditPass(ShopAuditLog shopAuditLog) {
        // 参数校验
        if (StringUtils.isEmpty(shopAuditLog.getNote())) {
            throw new BusinessException("备注不能为空");
        }
        // 审核人
        Emp audit = new Emp();
        audit.setUsername("tom");
        audit.setId(351L);
        audit.setPhone("18708146255");
        // 审核时间
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(audit.getId());
        shopAuditLog.setState(1);
        // 添加店铺日志
        shopAuditLogMapper.add(shopAuditLog);
        // 改变店铺状态为已通过
        Shop shop = shopMapper.findById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        // 发送邮件
        //3.发送邮件通知 商家 审核失败，告知原因，修改的地址
        //复杂邮件对象
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            //使用工具类来构建消息
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //
            helper.setFrom("775366739@qq.com");
            //设置邮件主题
            helper.setSubject("激活通知");
            //邮件正文
            helper.setText("<h1>请点击以下链接激活</h1><br>"+
                    "<span>激活：</span><br>"+
                    "<a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击激活</a>",true);
            helper.setTo("775366739@qq.com");
            //发送消息
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void auditReject(ShopAuditLog shopAuditLog) {
        // 参数校验
        if (StringUtils.isEmpty(shopAuditLog.getNote())) {
            throw new BusinessException("备注不能为空");
        }
        // 审核
        shopAuditLog.setAuditId(351L);
        // 审核时间
        shopAuditLog.setAuditTime(new Date());
        // 添加店铺日志
        shopAuditLog.setState(-1);
        shopAuditLogMapper.add(shopAuditLog);
        // 设置店铺状态
        // 改变店铺状态为已通过
        Shop shop = shopMapper.findById(shopAuditLog.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);
    }

    @Override
    public void activate(Long id) {
        Shop shop = shopMapper.findById(id);
        shop.setState(3);
        shopMapper.update(shop);
    }

    @Override
    public AjaxResult echarts() {
        List<ShopStateVo> shopStateVos = shopMapper.findByState();
        List<Integer> states = shopStateVos.stream().map(ShopStateVo::getState).collect(Collectors.toList());
        List<Integer> counts = shopStateVos.stream().map(ShopStateVo::getCount).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("states",states);
        map.put("counts",counts);
        return AjaxResult.me().setResultObj(map);
    }
}
